/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.panels;

import java.awt.AlphaComposite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import org.jfonia.connect5.basics.MutableValueNode;
import org.jfonia.connect5.basics.Observer;
import org.jfonia.constants.ViewConstants;
import org.jfonia.view.main.LeadSheetPlayer;

/**
 *
 * @author Rik Bauwens
 */
public class PlayPanel extends BasicNodePanel implements Observer
{
        private LeadSheetPanel leadSheetPanel;
        private final double TOP_MARGIN = ViewConstants.FRAME_BOX_TOP_MARGIN
                                    + ViewConstants.FRAME_BOX_WIDTH
                                    + ViewConstants.FRAME_BOX_BOTTOM_MARGIN;
        private final double LEFT_MARGIN = ViewConstants.STAFF_BOX_LEFT_MARGIN
                                    + ViewConstants.STAFF_BOX_WIDTH
                                    + ViewConstants.STAFF_BOX_RIGHT_MARGIN;
        private double x;

        public PlayPanel(LeadSheetPanel leadSheetPanel,
                         MutableValueNode<Double> xNode, MutableValueNode<Double> yNode,
                         MutableValueNode<Double> width, MutableValueNode<Double> height)
        {
                super(xNode, yNode, width, height);
                setOpaque(false);
                setVisible(false);
                this.leadSheetPanel = leadSheetPanel;
                LeadSheetPlayer.getInstance().addObserver(this);
        }

        @Override
        public void paintComponent(Graphics g)
        {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
                g2.setColor(ViewConstants.PLAYPANEL_CARET_COLOR);
                g2.drawLine((int) x, getY() + (int) TOP_MARGIN, (int) x,
                            getY() + getHeight());
                g2.setColor(ViewConstants.PLAYPANEL_OVERLAY_COLOR);
                g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 
                                                    ViewConstants.PLAYPANEL_ALPHA));
                g2.fillRect(getX() + (int) LEFT_MARGIN, getY() + (int) TOP_MARGIN,
                            (int) (x - LEFT_MARGIN), getHeight()- (int) TOP_MARGIN);
        }

        public void onNotify(Object source)
        {
                LeadSheetPlayer leadSheetPlayer = (LeadSheetPlayer) source;
                if(leadSheetPlayer.isPlaying())
                {
                        setVisible(true);
                        x = leadSheetPanel.getX(((LeadSheetPlayer) source).getCurrentTick());
                        repaint();
                }
                else
                        setVisible(false);
                
        }
}
